﻿using System;
using Contoso.Threading;
namespace Contoso
{
    public static class UtilEx
    {
        private static readonly Random _r = new Random();

        public static void MakeRandomness(int count, ref long pBuf)
        {
            var buffer = new byte[count];
            pBuf = 0;
#if SQLITE_THREADSAFE
            var mutex = MutexEx.Alloc(MutexEx.MUTEX.STATIC_PRNG);
#endif
            MutexEx.Enter(mutex);
            while (count-- > 0)
                pBuf = (uint)((pBuf << 8) + (byte)_r.Next(byte.MaxValue));
            MutexEx.Leave(mutex);
        }

        public static void MakeRandomness(byte[] buffer, int Offset, int count)
        {
            var iBuf = DateTime.Now.Ticks;
#if SQLITE_THREADSAFE
            var mutex = MutexEx.Alloc(MutexEx.MUTEX.STATIC_PRNG);
#endif
            MutexEx.Enter(mutex);
            while (count-- > 0)
            {
                iBuf = (uint)((iBuf << 8) + (byte)_r.Next(byte.MaxValue));
                buffer[Offset++] = (byte)iBuf;
            }
            MutexEx.Leave(mutex);
        }
    }
}
